package com.example.im.controller;


import cn.hutool.core.bean.BeanUtil;
import com.example.im.domain.dto.CustGetDTO;
import com.example.im.domain.po.CustInfo;
import com.example.im.domain.vo.CustGetVO;
import com.example.im.service.CustInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
/**
 * @description: 客户基本信息管理接口
 * @Author: whopxx
 */
@RestController
@RequestMapping("/userinfo")
@Tag(name = "客户信息管理接口", description = "增删查改用户信息")
public class CustController {
    @Resource
    private CustInfoService custInfoService;


    /**
     * 查询客户信息
     */
    @PostMapping("/get")
    @Operation(summary = "查询客户信息")
    @ApiResponse(responseCode = "200", description = "查询客户信息成功")
    public CustGetVO getInfo(@ParameterObject @RequestBody CustGetDTO custGetDTO){
        long startTime = System.currentTimeMillis();
        if(ObjectUtils.isEmpty(custGetDTO.getCustId()) && ObjectUtils.isEmpty(custGetDTO.getMobile())){
            return null;
        }
        CustInfo custInfo = custInfoService.CacheCustInfo(custGetDTO);
        CustGetVO info = new CustGetVO();
        BeanUtil.copyProperties(custInfo, info);
        info.setSendNum(-1L);
        long endTime = System.currentTimeMillis();
        Double time = 1.0 * (endTime - startTime);
        info.setTotalTime(time);
        return info;
    }

    /**
     * 新增客户信息
     */
    @PostMapping("/add")
    @Operation(summary = "新增客户信息")
    @ApiResponse(responseCode = "200", description = "新增客户信息成功")
    public boolean add(@ParameterObject @RequestBody CustInfo custInfo){
        if (ObjectUtils.isEmpty(custInfo)){
            return false;
        }
        return custInfoService.save(custInfo);
    }

    /**
     * 更新客户信息
     */
    @PostMapping("/update")
    @Operation(summary = "更新客户信息")
    @ApiResponse(responseCode = "200", description = "更新客户信息成功")
    public boolean update(@ParameterObject @RequestBody CustInfo custInfo){
        if (ObjectUtils.isEmpty(custInfo) || ObjectUtils.isEmpty(custInfo.getId())){
            return false;
        }
        return custInfoService.updateById(custInfo);
    }

    /**
     * 删除客户信息
     */
    @PostMapping("/delete")
    @Operation(summary = "删除客户信息")
    @ApiResponse(responseCode = "200", description = "删除客户信息成功")
    public boolean delete(@ParameterObject @RequestBody CustInfo custInfo){
        if (ObjectUtils.isEmpty(custInfo) || ObjectUtils.isEmpty(custInfo.getId())){
            return false;
        }
        return custInfoService.removeById(custInfo.getId());
    }
}
